package com.leyou.page.controller;

import com.leyou.page.service.FileService;
import com.leyou.page.service.PageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

/**
 * @Author: taft
 * @Date: 2018-10-21 12:07
 */
@Controller
@RequestMapping("item")
public class PageController {


    @Autowired
    private PageService pageService;

    @Autowired
    private FileService fileService;


    @GetMapping("{id}.html")
    public String toItemPage(@PathVariable("id") Long id,Model model){

        Map<String, Object> map = pageService.loadData(id);

        model.addAllAttributes(map);

        // 判断是否需要生成新的页面
        if(!this.fileService.exists(id)){
            this.fileService.syncCreateHtml(id);
        }

        return "item";
    }
}
